package imaSync

import (
	"strconv"
	"vidii"

	"local/config"
	"local/services/imaSync/model"

	youduModel "youdu/model"

	"github.com/gin-gonic/gin"
)

func listOrg(c *gin.Context) {
	// 更新服务器地址确保是最新的配置
	youduOrg.Server = config.Inst.Entprise.YouduServer
	//
	ids := c.Query("id")
	id, err := strconv.Atoi(ids)
	if err != nil {
		vidii.ResponsePack(c, 1, "参数错误: "+ids+"#"+err.Error())
		return
	}
	list, err := youduOrg.DepartmentListGet(id)
	if err != nil {
		vidii.ResponsePack(c, 1, "获取部门列表失败: "+err.Error())
		return
	}
	vidii.ResponsePackData(c, 0, "ok", list)
}
func saveOrg(c *gin.Context) {
	// 更新服务器地址确保是最新的配置
	youduOrg.Server = config.Inst.Entprise.YouduServer
	//
	// 绑定参数到 youduModel.UserInfo
	var dept model.DeptInfo
	if err := c.Bind(&dept); err != nil {
		vidii.ResponsePack(c, 1, "参数错误: "+err.Error())
		return
	}
	var yd = youduModel.DepartmentInfo{
		Id:       dept.Id,
		Name:     dept.Name,
		ParentId: dept.ParentId,
		SortId:   dept.Sort,
	}
	err := youduOrg.DepartmentSave(yd)
	if err != nil {
		vidii.ResponsePack(c, 1, "部门保存失败: "+err.Error())
		return
	}
	// list, _ := youduOrg.UserInfoGet("vi3")
	// common.ResponsePackData(c, 0, "", dpid)
	vidii.ResponsePack(c, 0, "部门保存成功")
}
func delOrg(c *gin.Context) {
	// 更新服务器地址确保是最新的配置
	youduOrg.Server = config.Inst.Entprise.YouduServer
	//
	ids := c.Query("id")
	id, err := strconv.Atoi(ids)
	if err != nil {
		vidii.ResponsePack(c, 1, "参数错误: "+ids+"#"+err.Error())
		return
	}
	err = youduOrg.DepartmentDelete(id)
	if err != nil {
		vidii.ResponsePack(c, 1, "删除失败: "+err.Error())
		return
	}
	vidii.ResponsePack(c, 0, "ok")
}
func saveUser(c *gin.Context) {
	// 更新服务器地址确保是最新的配置
	youduOrg.Server = config.Inst.Entprise.YouduServer
	//
	var user model.UserInfo
	if err := c.Bind(&user); err != nil {
		vidii.ResponsePack(c, 1, "用户参数错误: "+err.Error())
		return
	}
	// 性别处理
	sex := 0
	if user.Gender == "2" {
		sex = 1
	}
	// 状态处理，只要这俩不等于1，就激活，
	state := 1
	if user.UserStatus == "1" {
		state = -1 //冻结就禁用
	} else if user.LoginFlag == "1" {
		state = 0 // 不能登录就未激活
	}

	yd := youduModel.UserInfo{
		UserId:      user.LoginName,
		Name:        user.Name,
		Dept:        []int{user.OrgId},
		Mobile:      user.Mobile,
		Email:       user.Email,
		Phone:       user.Phone,
		Gender:      sex,
		EnableState: state,
	}
	// fmt.Println(yd)
	if err := youduOrg.UserSave(yd); err != nil {
		vidii.ResponsePack(c, 1, "保存用户资料失败："+err.Error())
		return
	}
	vidii.ResponsePack(c, 0, " 保存用户资料成功")
}
func delUser(c *gin.Context) {
	// 更新服务器地址确保是最新的配置
	youduOrg.Server = config.Inst.Entprise.YouduServer
	//
	id := c.Query("loginName")
	if id == "" {
		vidii.ResponsePack(c, 1, "参数不能为空 ")
		return
	}
	err := youduOrg.UserDelete(id)
	if err != nil {
		vidii.ResponsePack(c, 1, "删除用户失败: "+err.Error())
		return
	}
	vidii.ResponsePack(c, 0, "删除用户成功")
}
